<?php

namespace app\models\base;

use app\api\modules\v1\market\models\Message;
use app\api\modules\v1\market\models\SessionMessage;
use Yii;

/**
 * This is the model class for table "{{%svn}}".
 *
 * @property integer $id
 * @property integer $project_id
 * @property integer $user_id
 * @property string $svn_path
 * @property string $local_path
 */
class Svn extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%svn}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['project_id', 'user_id', 'svn_path', 'local_path'], 'required'],
            [['project_id', 'user_id'], 'integer'],
            ['svn_path','match','pattern'=>'/^svn:\/\/(?:\d+\.)+\d+(?:\/[\w]+)+$/i','message'=>'不是有效的svn路径'],
            [['svn_path', 'local_path'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'project_id' => '项目',
            'user_id' => '用户',
            'svn_path' => 'Svn路径',
            'local_path' => '本地路径',
        ];
    }

    /**修改和添加
     * @param $post
     * @return bool
     */
    public static function saveSvn($post)
    {
        if(empty($post['svn_id'])){
            //添加
            $svn = new Self();

        }else{
            //修改
            $svn = self::findOne($post['svn_id']);
        }

        $svn->load($post,'');

        if($svn -> save()){
            return true;
        }else{
            SessionMessage::setMess(array_values($svn->getFirstErrors())[0]);
            return false;
        }
    }

    /**获取svn的信息
     * @param $get
     * @return bool
     */
    public static function getSvn($get)
    {
        $svn = self::find()->asArray()->where(['project_id'=>$get['project_id'],'user_id'=>$get['user_id']])->all();
        if(empty($svn)){
            SessionMessage::setMess(Message::EMPTY_DATA);
            return false;
        }

        return $svn;
    }

}
